//
//  FKChooseAttrCell.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/26.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKChooseAttrCell.h"
#import "FKAlignmentLayout.h"
#import "FKChooseAttrItemCell.h"
#import "FKCommoditySpecification.h"
static NSString *const ID = @"FKChooseAttrItemCell";

@interface FKChooseAttrCell ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *datas;

@end

@implementation FKChooseAttrCell

- (void)setupSubviews
{
    [super setupSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor999999 text:@"规格"];
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    FKAlignmentLayout *flowLayout = [[FKAlignmentLayout alloc] init];
    flowLayout.marginOfCell = 12;
    flowLayout.alignType = AlignLeft;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    [self.contentView addSubview:collectionView];
    self.collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.allowsMultipleSelection = false;
    [collectionView registerClass:[FKChooseAttrItemCell class] forCellWithReuseIdentifier:ID];
    collectionView.alwaysBounceVertical = true;
    collectionView.contentInset = UIEdgeInsetsMake(0, 12, 0, 12);
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.top.offset(0);
        make.height.mas_equalTo(fk_adjustW(46));
    }];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.top.equalTo(titleLabel.mas_bottom);
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKChooseAttrItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.model = self.datas[indexPath.item];
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKGoodsSpecificationValue *spec = self.datas[indexPath.item];
    return spec.itemSize;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKGoodsSpecificationValue *model = self.datas[indexPath.item];
    
    if ([self.delegate respondsToSelector:@selector(chooseAttrCell:didSelectedSpecValue:)]) {
        [self.delegate chooseAttrCell:self didSelectedSpecValue:model];
    }
    
    if (model.isSelected) return;
    
    for (FKGoodsSpecificationValue *spec in self.datas) {
        NSInteger index = [self.datas indexOfObject:spec];
        spec.selected = (index == indexPath.item);
    }
    
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
    
}

- (void)setData:(id)data
{
    [super setData:data];
    
    if ([data isKindOfClass:[FKCommoditySpecification class]]) {
        FKCommoditySpecification *model = (FKCommoditySpecification *)data;
        self.titleLabel.text = model.title;
        self.datas = model.value;
        [self.collectionView reloadData];
        
        if (self.datas.count > 0) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self collectionView:self.collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
                [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:false scrollPosition:UICollectionViewScrollPositionNone];
            });
        }
    }
}

@end
